"ui";
auto.waitFor()
setScreenMetrics(1080, 2400);

var common = require("./common/common.js");
ui.layout(
    <vertical>
        <card w="*" h="120" cardCornerRadius="2dp" cardElevation="1dp" foreground="?selectableItemBackground">
            <horizontal marginTop="50">
                <img w="60" h="60" marginLeft="20" src='file://./res/logo.png'/>
                <text marginTop="20" textSize="15" text="企业微信打卡"></text>
            </horizontal>
        </card>
        <horizontal margin="8 20 8">
            <text text="上班打卡时间：" textSize="14"></text>
            <input id="workTime" w="*" text='08:20:00' hint="实际打卡时间会在此基础上后延5-10分钟" enabled="false"/>
        </horizontal>
        <horizontal margin="8 20 8">
            <text text="下班打卡时间：" textSize="14"></text>
            <input id="freeTime" w="*" text='18:30:00' hint="实际打卡时间会在此基础上后延10-15分钟" enabled="false"/>
        </horizontal>
        <horizontal margin="8 20 8">
            <text text="打卡日：" textSize="14"></text>
            <input id="weekTime" w="*" text='一,二,三,四,五' hint="例如：一,二,三" enabled="false"/>
        </horizontal>
        <horizontal margin="8 20 8">
            <text text="锁屏密码：" textSize="14"></text>
            <input id="lockPwd" w="*" text='' hint="用于锁屏情况下自动解锁手机" password="true" enabled="false"/>
        </horizontal>
        <horizontal marginTop="20">
            <button w="50" h="35" marginLeft="130" id="config" text="修改" bg="#1E90FF"></button>
            <button w="50" h="35" marginLeft="40" id="sign" text="运行" bg="#66CD00"></button>
            <button w="80" h="35" marginLeft="60" id="taskList" text="查看任务列表" bg="#CDCDB4" enabled="false"></button>
        </horizontal>

        <card id="card1" w="*" h="120" cardCornerRadius="2dp" cardElevation="1dp" cardBackgroundColor="#3CB371" marginTop="220">
            <horizontal marginTop="10" marginLeft="20">
                <text textSize="10" text="任务id："></text>
                <text textSize="10" id="taskId" text=""></text>
            </horizontal>
            <horizontal marginTop="30" marginLeft="20">
                <text textSize="10" text="任务名称："></text>
                <text textSize="10" id="taskName" text=""></text>
            </horizontal>
            <horizontal marginTop="50" marginLeft="20">
                <text textSize="10" text="执行时间："></text>
                <text textSize="10" id="taskTime" text=""></text>
            </horizontal>
            <horizontal marginTop="70" marginLeft="20">
                <text textSize="10" text="执行周："></text>
                <text textSize="10" id="taskWeek" text=""></text>
            </horizontal>
            <horizontal marginTop="90" marginLeft="20">
                <text textSize="10" text="执行脚本："></text>
                <text textSize="10" id="taskScript" text=""></text>
            </horizontal>
        </card>
        <card id="card2" w="*" h="120" cardCornerRadius="2dp" cardElevation="1dp" cardBackgroundColor="#3CB371" marginTop="20">
            <horizontal marginTop="10" marginLeft="20">
                <text textSize="10" text="任务id："></text>
                <text textSize="10" id="taskId1" text=""></text>
            </horizontal>
            <horizontal marginTop="30" marginLeft="20">
                <text textSize="10" text="任务名称："></text>
                <text textSize="10" id="taskName1" text=""></text>
            </horizontal>
            <horizontal marginTop="50" marginLeft="20">
                <text textSize="10" text="执行时间："></text>
                <text textSize="10" id="taskTime1" text=""></text>
            </horizontal>
            <horizontal marginTop="70" marginLeft="20">
                <text textSize="10" text="执行周："></text>
                <text textSize="10" id="taskWeek1" text=""></text>
            </horizontal>
            <horizontal marginTop="90" marginLeft="20">
                <text textSize="10" text="执行脚本："></text>
                <text textSize="10" id="taskScript1" text=""></text>
            </horizontal>
        </card>
    </vertical>
)

let dataPath = './data/data.json'
let constantPath = './data/constant.json'
let constant = common.readFile(constantPath);
files.create(constant.logPath);
console.setGlobalLogConfig({"file": constant.logPath});

ui.card1.visibility=8;
ui.card2.visibility=8; 
var storage = storages.create("qywxtask")

var data = common.readFile(dataPath);
if (data.onTime) {
    ui.workTime.setText(data.onTime);
};
if (data.offTime) {
    ui.freeTime.setText(data.offTime);
};
if (data.week) {
    ui.weekTime.setText(data.week);
};
if (data.lockPwd) {
    ui.lockPwd.setText(data.lockPwd);
};

ui.config.click(function(){
    if (ui.config.text() == "修改") {
        if (ui.sign.text() == "停止") {
            toast("请先停止运行中的任务, 稍后再试！");
        } else {
            ui.config.setText("保存")
            ui.workTime.setEnabled(true);
            ui.freeTime.setEnabled(true);
            ui.weekTime.setEnabled(true);
            ui.lockPwd.setEnabled(true);
        }
    } else {
        ui.config.setText("修改")
        ui.workTime.setEnabled(false);
        ui.freeTime.setEnabled(false);
        ui.weekTime.setEnabled(false);
        ui.lockPwd.setEnabled(false);
        // 获取修改后的内容，更新配置文件data.json
        var data = common.readFile(dataPath);
        data.onTime = ui.workTime.text();
        data.offTime = ui.freeTime.text();
        data.week = ui.weekTime.text();
        data.lockPwd = ui.lockPwd.text();
        setTimeout(() => {
            common.updateFile(dataPath, data);
        }, 1000);
    }
    
})

ui.taskList.click(function(){
    if (ui.sign.text() == "停止") {
        if (ui.taskList.text() == "查看任务列表") {
            var tasks = storage.get("tasks", [])
            var onTask = tasks.on;
            var offTask = tasks.off;
            if (onTask) {
                ui.taskId.setText(onTask.id.toString());
                ui.taskName.setText(onTask.name);
                ui.taskTime.setText(onTask.time);
                ui.taskWeek.setText(onTask.week.toString());
                ui.taskScript.setText(onTask.script);
                ui.card1.visibility=0;
            }
            if (offTask) {
                ui.taskId1.setText(offTask.id.toString());
                ui.taskName1.setText(offTask.name);
                ui.taskTime1.setText(offTask.time);
                ui.taskWeek1.setText(offTask.week.toString());
                ui.taskScript1.setText(offTask.script);
                ui.card2.visibility=0;
            }
            ui.taskList.setText("隐藏任务列表");
        } else {
            ui.card1.visibility=8;
            ui.card2.visibility=8;
            ui.taskList.setText("查看任务列表");
        }
    }
})

function delTask(){
    var tasks = storage.get("tasks", []);
        var offTask = tasks.off;
        //console.log(onTask);
        //console.log(offTask);
        var onTask = tasks.on;
        // 删除任务
        if (onTask) {
            console.log("删除任务：", onTask.id);
            $timers.removeTimedTask(onTask.id)
        }
        if (offTask) {
            console.log("删除任务：", offTask.id)
            $timers.removeTimedTask(offTask.id)
        }
}

var thread = null
ui.sign.click(function(){
    if (ui.sign.text() == "运行") {
        if (ui.config.text() == "保存") {
            toast("请先保存配置, 稍后再试！");
        } else {
            delTask();
            thread = threads.start(function(){
                var onTime = ui.workTime.text().replace("：", ":");
                var offTime = ui.freeTime.text().replace("：", ":");
                var week = ui.weekTime.text().replace("，", ",");
                var lockPwd = ui.lockPwd.text();
                common.writeFile(dataPath, {"onTime": onTime, "offTime": offTime, "week": week, "lockPwd": lockPwd})
                var weekList = week.split(",")
                var taskInfo = {"lockPwd": lockPwd}
                if (onTime) {
                    var timeTask1 = setTimeTask(weekList, onTime, "./workSign.js");
                    taskInfo.on = {}
                    taskInfo.on.id = timeTask1.id
                    taskInfo.on.name = "上班打卡"
                    taskInfo.on.week = week
                    taskInfo.on.time = onTime
                    taskInfo.on.script = timeTask1.scriptPath
                }
                if (offTime) {
                    var timeTask2 = setTimeTask(weekList, offTime, "./freeSign.js");
                    taskInfo.off = {}
                    taskInfo.off.id = timeTask2.id
                    taskInfo.off.name = "下班打卡"
                    taskInfo.off.week = week
                    taskInfo.off.time = offTime
                    taskInfo.off.script = timeTask2.scriptPath
                }
                storage.put("tasks", taskInfo)
            })
            ui.taskList.attr("bg","#FFB90F");
            ui.taskList.setEnabled(true);
            ui.sign.setText("停止");
        }
    } else {
        delTask();
        storage.remove("tasks");
        thread.interrupt();

        // 隐藏任务列表
        ui.card1.visibility=8;
        ui.card2.visibility=8;

        // 禁止任务列表按钮的点击事件
        ui.taskList.attr("bg","#CDCDB4");
        ui.taskList.setEnabled(false);
        ui.taskList.setText("查看任务列表");

        toast("任务终止");
        ui.sign.setText("运行");
    }
})

function setTimeTask(weeks, time, script) {
    var timeTask = $timers.addWeeklyTask({
        path: script,
        // 时间戳为Mon Jun 21 2021 13:14:00 GMT+0800 (中国标准时间)，事实上只有13:14:00的参数起作用
        time: time,
        daysOfWeek: weeks,
        delay: 0,
        loopTimes: 1,
        interval: 0
    })
    return timeTask
}

